import Router from '@koa/router'
import { map, pick } from 'lodash'
import {
    eq,
    insert,
    like,
    select,
    delete as sqlDelete,
    update,
    in  as $in,
} from 'mysql-bricks'
import { query } from '../models'
import dayjs from 'dayjs'

/**
 * 获取停车记录列表
 */
export const getParkRecordPageController: Router.Middleware = async (ctx) => {
    const carIds = ctx.query.carIds?.toString() || ''
    const carIdsArr = carIds.split(',').map((item) => +item)
    const whereIn = carIdsArr.length > 1 ? $in('carId', carIdsArr) : undefined
    const page = ctx.query.page || 1
    // 生成sql
    const sql = select(
        'park_record.*',
        'car.carNum as carNum',
        'park.name as parkName',
        'pay_record.num as payRecordNum',
        'pay_record.status as payRecordStatus',
        'pay_record.realAmount as payRecordAmount'
    )
        .from('park_record')
        .where(whereIn)
        .limit(+ctx.query.limit || 10)
        .offset((+page - 1) * 10)
        .leftJoin('car', { carId: 'car.id' })
        .leftJoin('park', { parkId: 'park.id' })
        .leftJoin('pay_record', { payRecordId: 'pay_record.id' })
        .orderBy('createdAt DESC')
        .toString()
    console.log(sql)
    // 执行sql
    const res = await query(sql)
    const total = await query('select count(*) as total from park_record')
    // 返回数据
    ctx.body = {
        total: total[0].total,
        rows: res,
    }
}

export const getParkRecordById: Router.Middleware = async (ctx) => {
    const id = +ctx.params.id!
    // 生成sql
    const sql = select(
        'park_record.*',
        'car.carNum as carNum',
        'park.name as parkName',
        'pay_record.num as payRecordNum',
        'pay_record.status as payRecordStatus',
        'pay_record.realAmount as payRecordAmount'
    )
        .from('park_record')
        .where({ 'park_record.id': id })
        .leftJoin('car', { carId: 'car.id' })
        .leftJoin('park', { parkId: 'park.id' })
        .leftJoin('pay_record', { payRecordId: 'pay_record.id' })
        .toString()
    // 执行sql
    const res = await query(sql)
    ctx.body = res[0]
}

/**
 * 获取停车记录下拉框列表
 */
export const getParkRecordOptionListController: Router.Middleware = async (
    ctx
) => {
    // 生成sql
    const sql = select('id', 'name').from('park_record').toString()
    // 执行sql
    const res = (await query(sql)) as any

    // 返回数据
    ctx.body = res
}

/**
 * 添加停车记录
 */
export const postParkRecordController: Router.Middleware = async (ctx) => {
    // 获取请求参数
    const data = ctx.request.body
    // 生成sql
    const sql = insert().into('park_record').values(data).toString()
    // 执行sql
    const res = (await query(sql)) as any
    // 返回数据
    ctx.body = {
        id: res.insertId,
    }
}

/**
 * 修改停车记录信息
 */
export const putParkRecordByIdController: Router.Middleware = async (ctx) => {
    const id = +ctx.params.id!
    const data = ctx.request.body
    // 生成sql
    const sql = update('park_record', data).where('id', id).toString()
    // 执行sql
    const res = await query(sql)
    // 返回数据
    ctx.body = true
}

/**
 * 删除停车记录
 */
export const deleteParkRecordByIdController: Router.Middleware = async (
    ctx
) => {
    const id = +ctx.params.id!
    // 生成sql
    const sql = sqlDelete().from('park_record').where('id', id).toString()
    // 执行sql
    const res = await query(sql)
    // 返回数据
    ctx.body = true
}

/**
 * 获取最后一条停车记录
 */
export const getLastParkRecordPageController: Router.Middleware = async (
    ctx
) => {
    const carNum = ctx.query.carNum.toString()
    // 生成sql
    const sql =`SELECT park.id,park_record.*
    FROM car
    LEFT JOIN park_record ON car.id = park_record.carId 
    LEFT JOIN park ON park_record.parkId = park.id
    WHERE car.carNum = '${carNum}' 
    ORDER BY enterTime DESC 
    LIMIT 1;`
    console.log(sql)
    // 执行sql
    const res = (await query(sql)) as any
    console.log(res)
    // 返回数据
    ctx.body = {
       ...res[0],
    }
}
